<template>
  <div class="check-container">
    <div class="left-check">
      <el-table
          ref="singleTable"
          :data="adjustList"
          tooltip-effect="dark"
          style="width: 100%"
          highlight-current-row
          @current-change="handleCurrentChange"
          @selection-change="handleSelectionChange"
      >
        <el-table-column
            type="selection"
            align="center"
            width="55"
        ></el-table-column>
        <el-table-column
            :label="'已选(' + tableSelection.length + '/' + adjustList.length + ')'"
        >
          <template #default="scope">
            <el-tag
                :type="scope.row.conflictList.length === 0 ? 'success' : 'danger'"
                size="middle"
            >
              {{ scope.row.conflictList.length === 0 ? "无冲突" : "有冲突" }}
            </el-tag>
            {{ scope.row.blockId }}
          </template>
        </el-table-column>
        <el-table-column label="操作类型" width="80" align="center">
          <template #default="scope">{{ scope.row.adjustType }}</template>
        </el-table-column>
        <el-table-column label="目标" width="80" align="center">
          <template #default="scope">{{ scope.row.targetRowId }}</template>
        </el-table-column>
        <el-table-column label="校验结果" width="80" align="center">
          <template #default="scope">{{ scope.row.conflictList.length }}</template>
        </el-table-column>
      </el-table>
    </div>
    <div class="right-check" v-if="selectRow">
      <div class="terms-info clearfix">
        冲突列表({{ selectRow.conflictList.length }})
        <el-button type="primary" class="conflict-btn" @click="ignoreConflictAll">
          全部忽略
        </el-button>
        <el-button type="primary" class="conflict-btn" @click="checkAdjustResult">
          重新校验
        </el-button>
      </div>
      <div class="terms-list">
        <el-scrollbar class="modify-scroll" style="height: 100%">
          <div
              class="term-item"
              v-for="(conflictItem, index) in selectRow.conflictList"
              :key="index"
          >
            <h3 class="conflict-title">{{ index + 1 + "." + conflictItem.conflictType }}</h3>
            <p class="conflict-desc">{{ conflictItem.conflictDesc }}</p>

            <el-button
                :disabled="conflictItem.isIgnore"
                class="btn-ignore"
                type="primary"
                @click="ignoreConflictItem(conflictItem)"
            >
              {{ conflictItem.isIgnore ? "已忽略" : "忽略" }}
            </el-button>
          </div>
        </el-scrollbar>
      </div>

      <div class="check-result-info clearfix">
        <el-tag
            :type="selectRow.conflictList.length === 0 ? 'success' : 'danger'"
            size="middle"
        >
          {{ selectRow.conflictList.length === 0 ? "校验通过" : "检验不通过" }}
        </el-tag>
        <el-button type="primary" class="btn-check" @click="checkAndInsert">
          确定调整
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick } from 'vue';
import { cloneDeep } from 'lodash';
import { checkConflict } from '@/utils/tools.ts';
import {useGanttSettingStore} from "@/store/modules/ganttSettingStore/ganttSettingStore.ts";

const adjustList = ref<any[]>([]);
const tableSelection = ref<any[]>([]);
const selectRow = ref<any | null>(null);
const singleTable = ref<any>(null);

const ganttSettingStore = useGanttSettingStore();
const { currentBlock, currentRow, targetBlock, targetRow, showRowList, showMovedBlock } = ganttSettingStore

const { setShowRowList, setRawRowList } = ganttSettingStore

const calcConflictList = () => {
  adjustList.value = [];
  if (targetRow.value && currentBlock.value) {
    const adjustOjb = checkConflict(currentBlock.value, targetRow.value, targetBlock.value ? targetBlock.value : null);
    adjustList.value.push(adjustOjb);
  }
  if (currentRow.value && targetBlock.value) {
    const adjustOjb = checkConflict(targetBlock.value, currentRow.value, currentBlock.value ? currentBlock.value : null);
    adjustList.value.push(adjustOjb);
  }

  if (adjustList.value.length) {
    nextTick(() => {
      singleTable.value.setCurrentRow(adjustList.value[0]);
      singleTable.value.toggleAllSelection();
    });
  }
};

const handleSelectionChange = (val: any[]) => {
  tableSelection.value = val;
};

const handleCurrentChange = (val: any) => {
  selectRow.value = val;
};

const ignoreConflictItem = (item: any) => {
  item.isIgnore = true;
};

const ignoreConflictAll = () => {
  if (selectRow.value) {
    selectRow.value.conflictList.forEach(conflictItem => {
      conflictItem.isIgnore = true;
    });
  }
};

const checkAdjustResult = () => {
  if (!tableSelection.value.length) {
    alert('至少选择一项进行重新校验！');
    return;
  }

  tableSelection.value.forEach(adjustObj => {
    const blockId = adjustObj.blockId;
    let adjustIndex = 0;
    adjustList.value.forEach((rawAdjustItem, rawIndex) => {
      if (blockId === rawAdjustItem.blockId) {
        adjustIndex = rawIndex;
      }
    });

    adjustObj.conflictList = adjustObj.conflictList.filter(item => {
      return item.isIgnore === false;
    });

    adjustList.value.splice(adjustIndex, 1, adjustObj);
  });

  singleTable.value.setCurrentRow(adjustList.value[0]);
};

const checkAndInsert = () => {
  checkAdjustResult();
  if (!tableSelection.value.length) {
    return false;
  }

  const hasConflict = tableSelection.value.some(adjustObj => {
    return adjustObj.conflictList.length > 0;
  });

  if (hasConflict) {
    alert('调整任务存在时间冲突，请检查！');
    return false;
  } else {
    const rowList = cloneDeep(showRowList.value);
    tableSelection.value.forEach(adjustItem => {
      const currentRow = rowList.find(row => row.id === adjustItem.blockItem.parentId);

      if (showMovedBlock.value) {
        const movedBeforeBlock = currentRow.gtArray.find(blockItem => blockItem.id === adjustItem.blockId);
        if (movedBeforeBlock.movedStatus === 'after') {
          currentRow.gtArray = currentRow.gtArray.filter(blockItem => blockItem.id !== adjustItem.blockId);
        } else {
          movedBeforeBlock.movedStatus = 'before';
        }
      } else {
        currentRow.gtArray = currentRow.gtArray.filter(blockItem => blockItem.id !== adjustItem.blockId);
      }

      const newBlock = cloneDeep(adjustItem.blockItem);
      newBlock.movedStatus = 'after';
      newBlock.parentId = adjustItem.targetRowId;

      const targetRow = rowList.find(row => row.id === adjustItem.targetRowId);
      targetRow.gtArray.push(newBlock);
    });

    setShowRowList(rowList);
    emit('closeDialog');
  }
};

// 触发自定义事件
const emit = defineEmits(['closeDialog']);
</script>

<style lang="scss" scoped>
.check-container {
  display: flex;
  .left-check {
    width: 450px;
    padding-right: 10px;
    border-right: 2px solid #DDDDDD;
  }
  .right-check {
    flex: 1;
    padding-left: 15px;
    .adjust-blockItem {
      font-size: 16px;
    }
    .adjust-desc {
      font-size: 14px;
    }
    .terms-info {
      padding: 5px 0;
    }
    .conflict-btn {
      float: right;
      margin-left: 10px;
    }
    .terms-list {
      border: 1px solid #666666;
      height: 350px;
    }
    .term-item {
      padding: 10px 120px 10px 10px;
      position: relative;
      &:nth-child(2n+1) {
        background-color: #F4F4F4;
      }
    }
    .conflict-title {
      margin: 0;
      line-height: 1.5;
      font-size: 12px;
    }
    .conflict-desc {
      font-size: 12px;
      line-height: 1.5;
      margin: 0;
    }
    .btn-ignore {
      position: absolute;
      top: 20px;
      right: 20px;
    }
  }
  .check-result-info {
    padding-top: 15px;
    .btn-check {
      float: right;
    }
  }
}
</style>
